<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>日计划</title>
</head>
<body>
<div class="content-container" data-options="region:'center'">
<div class="panel-header"><div class="panel-title">日计划列表</div></div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>
	
	<div id="pro_tb">
		
				<div>
			日期：<input id="fromDate" class="easyui-datebox" /> ~ <input
				id="toDate" class="easyui-datebox" /> <a href="#"
				class="easyui-linkbutton" iconCls="icon-search" id="search">搜索</a>
		</div>
		<div>
		<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
		<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatepro()">编辑</a>
		<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletepro()">删除</a>
		</div>
	</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog"
		style="width: 800px; padding: 10px 20px;overflow-y:auto;top:30px;" closed="true"
		buttons="#pro-dlg-buttons">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">内容：</td>
						<td><input class="easyui-textbox"data-options="required:true,multiline:true" 
						id="content" name="content" style="width: 600px;height:100px;"/></td>
					</tr>
					<tr>
						<td style="width: 100px;">日期：</td>
						<td><input class="easyui-datebox" data-options="required:true,validType:'planexist',editable:false" id="plandate" name="plandate" style="width: 200px;"/></td>
					</tr>
					<tr>
						<td style="width: 100px;">形象进度：</td>
						<td><input class="easyui-textbox" data-options="required:true,validType:['number','percent']" id="percent" name="percent" style="width: 200px;"/>
							<label style="margin-left:70px;">主计划未完成形象进度：</label><label id="remainpercent"></label><label>%</label>
						</td>
						
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton ok" onclick="savePro()">保存</a> 
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
</body>
<script th:inline="javascript">
	var currentPro;
	var fromDate = ""; 
	var toDate = ""; 
	var planmain =[[${planmain}]];
	var planmainid= planmain.id;
	$(document).ready(function() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "plandaily",
			method : 'get',
			queryParams: {
				planmainid : planmainid,
				startdate : fromDate,
				enddate : toDate
			},
			onSelect : function(index, row) {
				currentPro = row;
			},
			onDblClickRow :function(index, row) {
				currentPro = row;
				updatepro();
			},
			columns : [ [ {
				field : 'content',
				title : '内容',
				width : '400px'
			}, {
				field : 'plandate',
				title : '日期',
				width : '100px',
				formatter : formatDate
			}, {
				field : 'percent',
				title : '形象进度',
				width : '',
				formatter : function(value, row, index) {
					return value+'%';
				}
			}, {
				field : 'finishpercent',
				title : '完成形象进度',
				width : '',
				formatter : function(value, row, index) {
					if(row.report){
						return (row.percent * row.report.percent/100).toFixed(2) +'%';
					}else{
						return '';
					}
					
				}
			},{
				field : 'reason',
				title : '拒绝理由',
				width : ''
			}, {
				field : 'status',
				title : '状态',
				width : '100px',
				formatter : function(value, row, index) {
					if(value==4){
						return value='已审';
					}else if(value==3){
						return value='已拒绝';
					}else if(value==6){
						return value='待二级审核';
					}
				}
			} ] ]
		});
	});

	function refreshProTable() {
		$('#pro_table').datagrid('load',{
			planmainid:planmainid,
			startdate:fromDate,
			enddate:toDate
		});
	}
	
	$("#search").click(function(){
		fromDate = $("#fromDate").datebox('getValue');
		toDate = $("#toDate").datebox('getValue');
		refreshProTable();
	});

	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加日计划');
		$("#proFm").form('clear');
		currentPro = {};
		getRemainPercent();
		$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
	}

	function updatepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		getRemainPercent();
		if (currentPro) {
			if(currentPro.status!=6){
			}
			
			$("#proFm").form('load', {
				content:currentPro.content,
				percent:currentPro.percent
			});
			$("#plandate").datebox("setValue",formatDate(currentPro.plandate));
			if(currentPro.status!=6){
				$("#proDlg").dialog('open').dialog('setTitle', '日计划详细');
				$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").hide();
			}else{
				$("#proDlg").dialog('open').dialog('setTitle', '更新日计划');
				$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
			}
		} else {
			$.messager.alert('更新', '请选择要更新的日计划', 'warning');
		}
	}
	
	function deletepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if(currentPro){
			if(currentPro.status==6){
				$.messager.confirm("操作提示", "您确定要执行操作吗？", function (data) {
		            if (data) {
						$.ajax({
							type : 'POST',
							url : "planDailyRemove.do?id="+currentPro.id,
							contentType : "application/json",
							success : function(data) {
								if (data.success) {
									$.messager.show({
										title : '删除',
										msg : '删除成功',
										timeout : 5000,
										showType : 'slide'
									});
									refreshProTable();
								} else {
									$.messager.alert('删除', '删除失败', 'error');
								}
							},
							dataType : "json"
						});
					}
		            else {
		            }
		        });
			}
			else{
				$.messager.alert('删除', '删除失败', 'error');
			}
		} else {
			$.messager.alert('更新', '请选择要删除的日计划', 'warning');
		}
	}
	
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var content = $("#content").textbox('getValue');
		currentPro.content = content;
		var plandate = $("#plandate").datebox('getValue');
		currentPro.plandate = plandate;
		var percent = $("#percent").textbox('getValue');
		currentPro.percent = percent;
		currentPro.mainid = planmain.id;
		$.ajax({
			type : 'POST',
			url : "planDailySave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success) {
					$.messager.show({
						title : '保存日计划',
						msg : '保存日计划成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				} else {
					$.messager.alert('保存日计划', '保存日计划失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	
	function getRemainPercent(){
		$.ajax({
			type : 'GET',
			url : "planmain/unfinishpercent?mainid="+planmain.id,
			contentType : "application/json",
			success : function(data) {
				if (data) {
					$("#remainpercent").text(data);
				}
			},
			dataType : "text"
		});
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(), month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(), hour = date.getHours(), min = date.getMinutes(), sec = date
				.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
	
	$.extend($.fn.textbox.defaults.rules, {
	     number: {//value值为文本框中的值
	           validator: function (value) {
	          var reg = /^\d+(\.\d+)?$/;
	           return reg.test(value);
	       },
        message : "请输入数字"
    },
    chinese : {
        validator : function(value, param) {
            var reg = /^[\u4e00-\u9fa5]+$/i;
            return reg.test(value);
        },
        message : "请输入中文"
    },
    checkLength: {
        validator: function(value, param){
            return param[0] >= get_length(value);
        },
        message: '请输入最大{0}位字符'
    },
    specialCharacter: {
        validator: function(value, param){
            var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
            return !reg.test(value);
        },
        message: '不允许输入特殊字符'
    },
    percent:{
    	validator: function (value, param) {
    		var percent = $("#remainpercent").text();
            return parseFloat(value)<=parseFloat(percent);
 
        },
        message: '不能超过剩余形象进度'
    },
    planexist:{
    	validator : function(value, param) {
	    	var data = {};
	    	if(currentPro.id){
	    		data.planid = currentPro.id;
	    	}else{
	    		data.planid= 0;
	    	}
	    	data.plandate = value;
	    	data.mainid = planmain.id;
    		var res = false;
    		$.ajax({
				type: 'POST',
				async : false,
				url: 'check/plandate',
				data: data,
				success: function(data) {
					res= data.success;
				},
				dataType: "json"
			});
			return res;
        },
        message : '已存在日计划'
    }
    });
</script>
</html>